from django.shortcuts import render
from dvadmin.utils.viewset import CustomModelViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from django.db.models import Q
from .models import Order
from .serializers import OrderSerializer, OrderCreateUpdateSerializer
from sales.models import SalesPerformance

class OrderViewSet(CustomModelViewSet):
    """
    订单管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
    create_serializer_class = OrderCreateUpdateSerializer
    update_serializer_class = OrderCreateUpdateSerializer
    filter_fields = ['status']  # 移除字段，通过自定义查询实现模糊搜索
    ordering = '-create_datetime'
    search_fields = ['order_number', 'customer_name', 'product', 'sales_person']  # 保留通用搜索
    permission_classes = []
    extra_filter_class = []
    
    # 配置导出字段
    export_field_label = {
        "order_number": "订单编号",
        "customer_name": "客户名称",
        "product": "产品名称",
        "quantity": "订单数量",
        "total_amount": "订单金额",
        "sales_person": "销售人员",
        "order_date": "下单日期",
        "delivery_date": "交付日期",
        "status": "订单状态",
        "priority": "优先级",
        "remarks": "备注"
    }
    
    # 配置导入字段
    import_field_dict = {
        "order_number": "订单编号",
        "customer_name": "客户名称",
        "product": "产品名称",
        "quantity": "订单数量",
        "total_amount": "订单金额",
        "sales_person": "销售人员",
        "order_date": "下单日期",
        "delivery_date": "交付日期",
        "status": {
            "title": "订单状态",
            "choices": {
                "data": {"待处理": 1, "处理中": 2, "已完成": 3, "已取消": 4},
            }
        },
        "priority": "优先级",
        "remarks": "备注"
    }

    def get_queryset(self):
        queryset = super().get_queryset()
        
        # 获取查询参数
        order_number = self.request.query_params.get('order_number', None)
        customer_name = self.request.query_params.get('customer_name', None)
        product = self.request.query_params.get('product', None)
        sales_person = self.request.query_params.get('sales_person', None)
        order_date = self.request.query_params.get('order_date', None)
        delivery_date = self.request.query_params.get('delivery_date', None)
        
        # 应用模糊搜索
        if order_number:
            queryset = queryset.filter(order_number__icontains=order_number)
        if customer_name:
            queryset = queryset.filter(customer_name__icontains=customer_name)
        if product:
            queryset = queryset.filter(product__icontains=product)
        if sales_person:
            queryset = queryset.filter(sales_person__icontains=sales_person)
        
        # 改进日期过滤逻辑，进行范围查询而非精确匹配
        if order_date:
            queryset = queryset.filter(order_date__gte=order_date)
        if delivery_date:
            queryset = queryset.filter(delivery_date__lte=delivery_date)
            
        return queryset

    def perform_create(self, serializer):
        serializer.save(creator=self.request.user.username)
        
    def perform_update(self, serializer):
        serializer.save(modifier=self.request.user.username)
